package com.erpanderp.wam.asset.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.swing.SwingWorker;

import com.erpanderp.wam.asset.model.entity.AssetDetails;
import com.erpanderp.wam.asset.model.entity.AssetRequest;
import com.erpanderp.wam.asset.model.service.AssetRequestDAO;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.EmailUtil;
import com.erpanderp.wam.util.WAMHostManager;

@ViewScoped
@ManagedBean
public class AssignAssetsBean implements Serializable{

	private static final long serialVersionUID = 1L;
	private HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
	private ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
	HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	private WAMUser wamUser = (WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
	private TimeZone timeZone = TimeZone.getDefault();
	
	private String assetCode;
	private String activity;
	private boolean FORM_VALID;
	private boolean SHOW_STATUS;
	private EmailUtil emailUtil;
	private String EMAIL_MESSAGE;
	private boolean renderBackButton; 
	private boolean renderCloseButton; 
	
    private AssetRequest assetRequest;
    private AssetDetails viewAssetMaster=new AssetDetails();
	
	private AssetRequestDAO assetRequestDAO =new AssetRequestDAO();
	
	private List<AssetDetails> assetDetailsList = new ArrayList<AssetDetails>(); 
	
	public AssignAssetsBean() {
		assetCode=request.getParameter("assetCode");		
		activity=request.getParameter("activity");
		assetRequest = assetRequestDAO.getAssetRequestByAssetCode(assetCode);
		System.out.println("ASSET ID:==========="+assetRequest.getAssetRequestId());
		//assetDetailsList = assetRequestDAO.getAllApprovedAssetDetailsByAssetCode(assetRequest.getAssetRequestId());
		if(activity!=null){
			assetDetailsList = assetRequestDAO.getAllAssignedAssets(assetRequest.getAssetRequestId());
		}else{
			assetDetailsList = assetRequestDAO.getAllApprovedAssetDetailsByAssetCode(assetRequest.getAssetRequestId());
		}
		
		if(wamUser != null){
			setRenderBackButton(true);
			setRenderCloseButton(false);
		}else{				
			setRenderCloseButton(true);
			setRenderBackButton(false);
		}
	}
	
	 //ASSIGN ASSET 
	 public String assetAssignmentAction() {		 
		 try{
			
			  Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
			  String assetType = params.get("assetType");	
			  String assetCode = params.get("assetCode");
			  String assetDetailsId = params.get("assetDetailsId");
			  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("assetType", assetType);
			  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("assetCode", assetCode);
			  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("assetDetailsId", assetDetailsId);
			  FacesContext.getCurrentInstance().getExternalContext().redirect("/wam/asset/assetassignedbysysadmin.html?assetType="+assetType+"&assetCode="+assetCode+"&assetDetailsId="+assetDetailsId);
	       }catch(Exception e){
	    	   e.printStackTrace();
		   }
		 return "assetassignedbysysadmin";	 
	}
	 
	 public String sendMailToCandidateAction(){				
		//MAIL TRIGGERING CODE
		System.out.println("AT CALLER METHOD: " + sendMessage());  //*******DONT REMOVE*******
		SHOW_STATUS=true;
		FacesContext.getCurrentInstance().addMessage("", new FacesMessage(FacesMessage.SEVERITY_INFO, "You have sent successfully.", ""));
		return null;
				
	 }
	 
	//SENDING EMAIL 
		public String sendMessage(){
			Map<String, String> hostsMap = WAMHostManager.getWAMHosts(context);
			String domainName = context.getInitParameter("domainName");
			String messageString ="";	
			
			String candidateEmail=null;
			String reocommenderEmail=null;
			String approverEmail=null;
			
			emailUtil = new EmailUtil();
			if (hostsMap.get(CommonLookup.DEV_EMAIL) == null) {
				candidateEmail= assetRequest.getEmployee().getEmail();
				reocommenderEmail= assetRequest.getRecommendedBy().getEmail();
				approverEmail= assetRequest.getApprovedBy().getEmail();
				
			}else{				
				candidateEmail=hostsMap.get(CommonLookup.DEV_EMAIL);
				reocommenderEmail= hostsMap.get(CommonLookup.DEV_EMAIL);
				approverEmail= hostsMap.get(CommonLookup.DEV_EMAIL);
			}
			
			String[] to={candidateEmail};
			String[] cc={reocommenderEmail,approverEmail};
			String requestURL = "asset/assignassets.html?assetCode=" + assetCode+"&activity=viewEmployee" ;
			String privateLink = "http://" + hostsMap.get(CommonLookup.PRIVATEHOST) + "/" + domainName + "/" + requestURL;
			String publicLink = "http://" + hostsMap.get(CommonLookup.PUBLICHOST) + "/"	+ domainName + "/" + requestURL;
			
			messageString = "Dear " + assetRequest.getEmployee().getFirstName() + " " + assetRequest.getEmployee().getLastName() + "" +
						    ",<br /><br />" +
						    "<b>Assets assigned .</b><br /><br />" +
							
						    "<a style='color:#FF9933;font-weight: bold;' href=\""+
						        privateLink + "\">" + "Please click here to know asset(s) details" + "</a /><br />"+
								"(Employees who are in Hyderabad office)"+"<br /><br />"+
								"<a style='color:#FF9933;font-weight: bold;' href=\""+
								publicLink + "\">" + "Please click here to know asset(s) details" + "</a /><br />"+
								"(out side Hyderabad office)"+"<br /><br />"+
								
								"Thanks & Regards, <br />"+CommonLookup.WAM_DEVTEAM_SIGNATURE;
					
					emailUtil.setMessage(messageString);
					emailUtil.setCCAddresses(cc);
					emailUtil.setRecieverAddresses(to);
					emailUtil.setSenderEmail("podquick@erpanderp.com", "WAM - Dev Team");
					emailUtil.setSubject("Assigned Asset(s) Details of "+assetRequest.getEmployee().getFirstName()+" "+assetRequest.getEmployee().getLastName() +"( "+assetRequest.getAssetReferenceCode()+" )");
					
					SwingWorker worker = new SwingWorker<String, Object>() {
						@Override
						protected String doInBackground() throws Exception {
							EMAIL_MESSAGE = emailUtil.processAndSendEmail();
							return null;
						}

						@Override
						protected void done() {
								System.out.println("DONE " + EMAIL_MESSAGE);		
						}
					};
					worker.execute();
					System.out.println( "BEFORE RETURNING: " + EMAIL_MESSAGE);
					return EMAIL_MESSAGE;
				}
	
	//GENERATE SETTERS AND GETTERS
	public TimeZone getTimeZone() {
		return timeZone;
	}

	public void setTimeZone(TimeZone timeZone) {
		this.timeZone = timeZone;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public ServletContext getContext() {
		return context;
	}

	public void setContext(ServletContext context) {
		this.context = context;
	}

	public String getAssetCode() {
		return assetCode;
	}

	public void setAssetCode(String assetCode) {
		this.assetCode = assetCode;
	}
	public boolean isFORM_VALID() {
		return FORM_VALID;
	}

	public void setFORM_VALID(boolean fORM_VALID) {
		FORM_VALID = fORM_VALID;
	}

	public EmailUtil getEmailUtil() {
		return emailUtil;
	}

	public void setEmailUtil(EmailUtil emailUtil) {
		this.emailUtil = emailUtil;
	}

	public String getEMAIL_MESSAGE() {
		return EMAIL_MESSAGE;
	}

	public void setEMAIL_MESSAGE(String eMAIL_MESSAGE) {
		EMAIL_MESSAGE = eMAIL_MESSAGE;
	}

	public AssetRequest getAssetRequest() {
		return assetRequest;
	}

	public void setAssetRequest(AssetRequest assetRequest) {
		this.assetRequest = assetRequest;
	}

	public AssetRequestDAO getAssetRequestDAO() {
		return assetRequestDAO;
	}

	public void setAssetRequestDAO(AssetRequestDAO assetRequestDAO) {
		this.assetRequestDAO = assetRequestDAO;
	}

	public List<AssetDetails> getAssetDetailsList() {
		return assetDetailsList;
	}

	public void setAssetDetailsList(List<AssetDetails> assetDetailsList) {
		this.assetDetailsList = assetDetailsList;
	}

	public boolean isSHOW_STATUS() {
		return SHOW_STATUS;
	}

	public void setSHOW_STATUS(boolean sHOW_STATUS) {
		SHOW_STATUS = sHOW_STATUS;
	}

	public String getActivity() {
		return activity;
	}

	public void setActivity(String activity) {
		this.activity = activity;
	}

	public AssetDetails getViewAssetMaster() {
		return viewAssetMaster;
	}

	public void setViewAssetMaster(AssetDetails viewAssetMaster) {
		this.viewAssetMaster = viewAssetMaster;
	}

	public boolean isRenderBackButton() {
		return renderBackButton;
	}

	public void setRenderBackButton(boolean renderBackButton) {
		this.renderBackButton = renderBackButton;
	}

	public boolean isRenderCloseButton() {
		return renderCloseButton;
	}

	public void setRenderCloseButton(boolean renderCloseButton) {
		this.renderCloseButton = renderCloseButton;
	}


}
